Remove various redundant '@license' tags in file headers
[lhc/web/wiklou.git] / includes / specials / formfields / Licenses.php
1 <?php
2 /**
3 * License selector for use on Special:Upload.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
23 * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
24 */
25
26 /**
27 * A License class for use on Special:Upload
28 */
29 class Licenses extends HTMLFormField {
30 /** @var string */
31 protected $msg;
32
33 /** @var array */
34 protected $licenses = [];
35
36 /** @var string */
37 protected $html;
38 /**#@-*/
39
40 /**
41 * @param array $params
42 */
43 public function __construct( $params ) {
44 parent::__construct( $params );
45
46 $this->msg = empty( $params['licenses'] )
47 ? wfMessage( 'licenses' )->inContentLanguage()->plain()
48 : $params['licenses'];
49 $this->selected = null;
50
51 $this->makeLicenses();
52 }
53
54 /**
55 * @private
56 */
57 protected function makeLicenses() {
58 $levels = [];
59 $lines = explode( "\n", $this->msg );
60
61 foreach ( $lines as $line ) {
62 if ( strpos( $line, '*' ) !== 0 ) {
63 continue;
64 } else {
65 list( $level, $line ) = $this->trimStars( $line );
66
67 if ( strpos( $line, '|' ) !== false ) {
68 $obj = new License( $line );
69 $this->stackItem( $this->licenses, $levels, $obj );
70 } else {
71 if ( $level < count( $levels ) ) {
72 $levels = array_slice( $levels, 0, $level );
73 }
74 if ( $level == count( $levels ) ) {
75 $levels[$level - 1] = $line;
76 } elseif ( $level > count( $levels ) ) {
77 $levels[] = $line;
78 }
79 }
80 }
81 }
82 }
83
84 /**
85 * @param string $str
86 * @return array
87 */
88 protected function trimStars( $str ) {
89 $numStars = strspn( $str, '*' );
90 return [ $numStars, ltrim( substr( $str, $numStars ), ' ' ) ];
91 }
92
93 /**
94 * @param array &$list
95 * @param array $path
96 * @param mixed $item
97 */
98 protected function stackItem( &$list, $path, $item ) {
99 $position =& $list;
100 if ( $path ) {
101 foreach ( $path as $key ) {
102 $position =& $position[$key];
103 }
104 }
105 $position[] = $item;
106 }
107
108 /**
109 * @param array $tagset
110 * @param int $depth
111 */
112 protected function makeHtml( $tagset, $depth = 0 ) {
113 foreach ( $tagset as $key => $val ) {
114 if ( is_array( $val ) ) {
115 $this->html .= $this->outputOption(
116 $key, '',
117 [
118 'disabled' => 'disabled',
119 'style' => 'color: GrayText', // for MSIE
120 ],
121 $depth
122 );
123 $this->makeHtml( $val, $depth + 1 );
124 } else {
125 $this->html .= $this->outputOption(
126 $val->text, $val->template,
127 [ 'title' => '{{' . $val->template . '}}' ],
128 $depth
129 );
130 }
131 }
132 }
133
134 /**
135 * @param string $message
136 * @param string $value
137 * @param null|array $attribs
138 * @param int $depth
139 * @return string
140 */
141 protected function outputOption( $message, $value, $attribs = null, $depth = 0 ) {
142 $msgObj = $this->msg( $message );
143 $text = $msgObj->exists() ? $msgObj->text() : $message;
144 $attribs['value'] = $value;
145 if ( $value === $this->selected ) {
146 $attribs['selected'] = 'selected';
147 }
148
149 $val = str_repeat( /* &nbsp */ "\xc2\xa0", $depth * 2 ) . $text;
150 return str_repeat( "\t", $depth ) . Xml::element( 'option', $attribs, $val ) . "\n";
151 }
152
153 /**#@-*/
154
155 /**
156 * Accessor for $this->licenses
157 *
158 * @return array
159 */
160 public function getLicenses() {
161 return $this->licenses;
162 }
163
164 /**
165 * Accessor for $this->html
166 *
167 * @param bool $value
168 *
169 * @return string
170 */
171 public function getInputHTML( $value ) {
172 $this->selected = $value;
173
174 $this->html = $this->outputOption( wfMessage( 'nolicense' )->text(), '',
175 (bool)$this->selected ? null : [ 'selected' => 'selected' ] );
176 $this->makeHtml( $this->getLicenses() );
177
178 $attribs = [
179 'name' => $this->mName,
180 'id' => $this->mID
181 ];
182 if ( !empty( $this->mParams['disabled'] ) ) {
183 $attibs['disabled'] = 'disabled';
184 }
185
186 return Html::rawElement( 'select', $attribs, $this->html );
187 }
188 }